package com.lg.concurrent.schedule;

import lombok.SneakyThrows;

import java.util.concurrent.CountDownLatch;

/**
 *类说明：演示CountDownLatch用法，
 * 共5个初始化子线程，6个闭锁扣除点，扣除完毕后，主线程和业务线程才能继续执行
 *
 */
public class CountDownLatchDemo2 {
   
    static CountDownLatch latch = new CountDownLatch(6);

    private static class InitThread implements Runnable{

        @Override
        public void run() {
           System.out.println("Thread_"+Thread.currentThread().getId()
                 +" ready init work......");
            latch.countDown();
            for(int i =0;i<2;i++) {
               System.out.println("Thread_"+Thread.currentThread().getId()
                     +" ........continue do its work");
            }
        }
    }
    
    private static class BusiThread implements Runnable{
        @Override
        public void run() {
            try {
                latch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            for(int i =0;i<3;i++) {
               System.out.println("BusiThread_"+Thread.currentThread().getId()
                     +" do business-----");
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        new Thread(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                Thread.sleep(1);
                System.out.println("Thread_"+Thread.currentThread().getId()
                     +" ready init work step 1st......");
                latch.countDown();
                Thread.sleep(1);
                System.out.println("Thread_"+Thread.currentThread().getId()
                     +" ready init work step 2nd......");
                latch.countDown();
            }
        }).start();
        new Thread(new BusiThread()).start();
        for(int i=0;i<=3;i++){
            Thread thread = new Thread(new InitThread());
            thread.start();
        }

        latch.await(); // 不要写成了latch.wait()
        System.out.println("Main do ites work........");
    }
}